import {
    selectDetails,
    selectList,
    disabledTMWLogin,
    enableTMWLogin,
    TMWLoginAdd,
    TMWLoginEdit,
    deleteTask,
  } from '@/services/ExpandFunction/TMWLogin';
  import prompt from '@/components/Prompt';
  
  export default {
    namespace: 'TMWLogin',
  
    state: {
      selectList: {},
      selectDetails: {},
    },
  
    effects: {
      *selectList({ payload }, { call, put }) {
        const response = yield call(selectList, payload);
        if (response.code === 0) {
          const { list, pageSize, total, pageNum } = response.data;
          yield put({
            type: 'list',
            payload: {
              selectList: {
                pagination: {
                  current: pageNum,
                  pageSize,
                  total,
                },
                list,
              },
            },
          });
        }
      },
     
      *TMWLoginOperate({ payload, callback }, { call }) {
        const response = yield call(payload.id ? TMWLoginEdit : TMWLoginAdd, payload);
        const message = response.message;
        if (response.code === 0) {
          prompt({ content: message });
          callback(response);
        }
      },
      *selectDetails({ payload, callback }, { call, put }) {
        const response = yield call(selectDetails, payload);
        if (response.code === 0) {
          yield put({
            type: 'detail',
            payload: {
              selectDetails: { [payload.id]: response.data },
            },
          });
          if (callback) callback(response.data);
        }
      },
      *disabledTMWLogin({ payload, callback }, { call, put }) {
        const response = yield call(disabledTMWLogin, payload);
        if (response.code === 0) {
          prompt({ content: response.message });
          callback(response);
        }
      },
      *enableTMWLogin({ payload, callback }, { call, put }) {
        const response = yield call(enableTMWLogin, payload);
        if (response.code === 0) {
          prompt({ content: response.message });
          callback(response);
        }
      },
      *deleteTask({ payload, callback }, { call }) {
        const response = yield call(deleteTask, payload);
        const { code, message } = response;
        const content = `删除${message}`;
        callback && callback(true);
        if (code !== 0) return;
        Prompt({ content });
        callback && callback(true);
      },
    },
  
    reducers: {
      list(state, { payload }) {
        return {
          ...state,
          selectList: payload.selectList,
        };
      },
      
      NoBinding(state, { payload }) {
        return {
          ...state,
          selectListNoBinding: payload.selectListNoBinding,
        };
      },
      detail(state, { payload }) {
        return {
          ...state,
          selectDetails: { ...state.selectDetails, ...payload.selectDetails },
        };
      },
    },
  };
  